<!doctype html>
<html lang="zh-cn">
  <head>
  <meta charset="utf-8">
<title>老知识迁移（二） - Huanghs blog</title>
<meta name="viewport" content="width=device-width, initial-scale=1">


  <link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png?v=1">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png?v=1">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png?v=1">
  <link rel="manifest" href="/favicon/site.webmanifest?v=1">
  
    <link rel="mask-icon" href="/favicon/safari-pinned-tab.svg?v=1" color="#ffffff">
    <link rel="shortcut icon" href="/favicon/favicon.ico?v=1">
    <meta name="msapplication-config" content="/favicon/browserconfig.xml?v=1">
  
  <meta name="msapplication-TileColor" content="#ffffff">
  <meta name="theme-color" content="#ffffff">

<meta name="generator" content="Hugo 0.66.0" /><meta itemprop="name" content="老知识迁移（二）">
<meta itemprop="description" content="从本地整合的一些年代久远的知识">
<meta itemprop="datePublished" content="2020-03-15T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2020-03-15T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="495">
<meta itemprop="image" content="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/20200315231938.png">



<meta itemprop="keywords" content="Django," /><meta property="og:title" content="老知识迁移（二）" />
<meta property="og:description" content="从本地整合的一些年代久远的知识" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://hhs44.github.io/blog/%E8%80%81%E7%9F%A5%E8%AF%86%E8%BF%81%E7%A7%BB%E4%BA%8C/" />
<meta property="og:image" content="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/20200315231938.png" />
<meta property="article:published_time" content="2020-03-15T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-03-15T00:00:00+00:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/20200315231938.png"/>

<meta name="twitter:title" content="老知识迁移（二）"/>
<meta name="twitter:description" content="从本地整合的一些年代久远的知识"/>
<meta name="twitter:site" content="@example"/>
<link rel="stylesheet" href="/css/bundle.min.b2b2abee095149a60c0a1a2c4fab4b7c140b32ca88abc3d2f83d0dd903b6f634.css" integrity="sha256-srKr7glRSaYMChosT6tLfBQLMsqIq8PS&#43;D0N2QO29jQ=">
        <link rel="stylesheet" href="/css/add-on.css">

</head>

  <body>
    
<header id="site-header">
  <nav id="site-nav">
    <h1 class="nav-title">
      <a href="/">
        
          
            博客
          
        
      </a>
    </h1>
    <menu id="site-nav-menu" class="flyout-menu">
      
        
          
          
            <a href="/" class="link"><i class='fa fa-home'></i> 首页</a>
          
        
      
        
          
          
            <a href="/blog/" class="link"><i class='far fa-newspaper'></i> 博客</a>
          
        
      
        
          
          
            <a href="/categories/" class="link"><i class='fas fa-sitemap'></i> 分类</a>
          
        
      
        
          
          
            <a href="/tags/" class="link"><i class='far fa-envelope'></i> 标签</a>
          
        
      
        
          
          
            <a href="/about/" class="link"><i class='far fa-id-card'></i> 关于</a>
          
        
      
      <a href="#share-menu" class="share-toggle"><i class="fas fa-share-alt">&nbsp;</i>Share</a>
      

    </menu>
    

    <a href="#share-menu" class="share-toggle"><i class="fas fa-share-alt fa-2x">&nbsp;</i></a>
    <a href="#lang-menu" class="lang-toggle" lang="zh-cn">zh-cn</a>
    <a href="#site-nav" class="nav-toggle"><i class="fas fa-bars fa-2x"></i></a>
  </nav>
  <menu id="lang-menu" class="flyout-menu">
  <a href="#" lang="zh-cn" class="link active">中文 (zh-cn)</a>
  
    
      
        <a href="/en" lang="en" class="no-lang link">English (en)</a>
      
    
      
    
  
</menu>

  
    <menu id="share-menu" class="flyout-menu">
      <h1>Share Post</h1>
      





    </menu>
  
</header>


    <div id="wrapper">
      <section id="site-intro">
  <a href="/"><img src="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/head.jpg" class="circle" width="" alt="huang`s blog " /></a>
  <header>
    <h1>Huanghs的个人博客 </h1>
  </header>
  <main>
    <p>不写下一些东西，就没法证明自己<br> 不多走一些路，就没法看到更多的风景</p>
  </main>
  
    <footer>
      <ul class="socnet-icons">
        

        <li><a href="//github.com/hhs44/hhs44.github.io" target="_blank" rel="noopener" title="GitHub" class="fab fa-github"></a></li>






<li><a href="//flickr.com/photos/example" target="_blank" rel="noopener" title="Flickr" class="fab fa-flickr"></a></li>




<li><a href="//linkedin.com/in/example" target="_blank" rel="noopener" title="LinkedIn" class="fab fa-linkedin"></a></li>
<li><a href="//linkedin.com/company/examplebusiness" target="_blank" rel="noopener" title="LinkedIn Company" class="fab fa-linkedin"></a></li>



<li><a href="//facebook.com/example" target="_blank" rel="noopener" title="Facebook" class="fab fa-facebook"></a></li>
<li><a href="//reddit.com/user/example" target="_blank" rel="noopener" title="Reddit" class="fab fa-reddit"></a></li>





<li><a href="weixin://contacts/profile/22" target="_blank" rel="noopener" title="WeChat" class="fab fa-weixin"></a></li>
<li><a href="//wpa.qq.com/msgrd?v=3&amp;uin=1159986871&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener" title="QQ" class="fab fa-qq"></a></li>
<li><a href="//instagram.com/example" target="_blank" rel="noopener" title="Instagram" class="fab fa-instagram"></a></li>

<li><a href="//twitter.com/example" target="_blank" rel="noopener" title="Twitter" class="fab fa-twitter"></a></li>


<li><a href="//pinterest.com/example" target="_blank" rel="noopener" title="Pinterest" class="fab fa-pinterest-p"></a></li>
<li><a href="//telegram.me/example" target="_blank" rel="noopener" title="telegram" class="fab fa-telegram"></a></li>



<li><a href="//researchgate.net/profile/example" target="_blank" rel="noopener" title="Research Gate"><i class="ai ai-researchgate"></i></a></li>
<li><a href="//keybase.io/example" target="_blank" rel="noopener" title="keybase" class="fab fa-keybase"></a></li>
<li><a href="//example.com/@example" target="_blank" rel="noopener" title="mastodon" class="fab fa-mastodon"></a></li>
<li><a href="mailto:1159986871@qq.com" target="_blank" title="Email" class="far fa-envelope"></a></li>

      </ul>
    </footer>
  
</section>


      <main id="site-main">
        <article class="post">
  <header>
  <div class="title">
    
        <h2><a href="/blog/%E8%80%81%E7%9F%A5%E8%AF%86%E8%BF%81%E7%A7%BB%E4%BA%8C/">老知识迁移（二）</a></h2>
    
    
        <p>从本地整合的一些年代久远的知识</p>
    
</div>
  <div class="meta">
    <time class="published" datetime="2020-03-15 00:00:00 &#43;0000 UTC">
      March 15, 2020
    </time>
    <span class="author">浩书</span>
    
      <p>3 分钟</p>
    
  </div>
</header>

  <section id="socnet-share">
    





  </section>
  
  <a href="/blog/%E8%80%81%E7%9F%A5%E8%AF%86%E8%BF%81%E7%A7%BB%E4%BA%8C/" class="image featured">
    
      <img src="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/20200315231938.png" alt="">
    
  </a>


  <div class="content">
    <h1 id="1django综述">1、Django综述</h1>
<h2 id="11框架结构">1.1框架结构</h2>
<p>模型层：抽象的（model）层，操作web应用的数据</p>
<p>视图层：（view）负责处理用户的请求并返回响应</p>
<p>模板层：（template）提供友好的语法渲染向用户呈现的信息</p>
<p>其他：</p>
<ul>
<li>
<p>常用web应用程序工具</p>
</li>
<li>
<p>其他核心功能</p>
</li>
<li>
<p>地理框架</p>
</li>
<li>
<p>性能和优化：有多种技术和工具，优化代码</p>
</li>
<li>
<p>国际化和本地化： 框架实现</p>
</li>
<li>
<p>表单：（from）</p>
</li>
<li>
<p>开发进程</p>
</li>
<li>
<p>安全：具有多种保护手段</p>
</li>
<li>
<p>管理：</p>
<ul>
<li>
<p>管理站点</p>
</li>
<li>
<p>管理动作</p>
</li>
<li>
<p>管理文档生成器</p>
</li>
</ul>
</li>
</ul>
<h1 id="2第一个django应用">2、第一个Django应用</h1>
<h2 id="21文件说明">2.1文件说明</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#版本查询</span>
python <span style="color:#f92672">-</span>m django <span style="color:#f92672">--</span>version

<span style="color:#75715e">#新建Django项目</span>
django<span style="color:#f92672">-</span>admin startproject xxxname(<span style="color:#960050;background-color:#1e0010">项目名</span>)

<span style="color:#75715e">#主项目文件</span>
__init__<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">说明</span>xxx是一个python包
setting<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">项目的配置文件</span>
urls<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">进行</span>url路由配置<span style="color:#960050;background-color:#1e0010">，像网站的目录</span>
wsgi<span style="color:#f92672">.</span>py:web服务器和django的交互入口,<span style="color:#960050;background-color:#1e0010">兼容</span>
manage<span style="color:#f92672">.</span>py<span style="color:#960050;background-color:#1e0010">：项目管理</span> <span style="color:#960050;background-color:#1e0010">管理</span>Django的命令行工具

  <span style="color:#75715e">#自带用于开发的简易服务器</span>
python manage<span style="color:#f92672">.</span>py runserver <span style="color:#ae81ff">8888</span><span style="color:#960050;background-color:#1e0010">（可更换端口）</span>
</code></pre></div><h2 id="heading"></h2>
<h2 id="22新建app">2.2新建app</h2>
<p>项目和应用：应用值专注于单一的应用程序，项目可以包含多个应用，一个应用也能被多个项目使用。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#新建app</span>
python3 manage<span style="color:#f92672">.</span>py startapp polls<span style="color:#960050;background-color:#1e0010">（</span>app名<span style="color:#960050;background-color:#1e0010">）</span>

<span style="color:#75715e">#app目录文件</span>
__init__<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">说明</span>xxx是一个python包
models<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">写和数据库项目相关内容</span>
views<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">接受请求，进行处理，与</span>M和T交互<span style="color:#960050;background-color:#1e0010">，返回</span>
<span style="color:#960050;background-color:#1e0010">答应，定义处理函数，视图函数</span>
tests<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">写测试代码</span>
admin<span style="color:#f92672">.</span>py:<span style="color:#960050;background-color:#1e0010">与网站后台管理相关文件</span>

<span style="color:#75715e">#建立应用和项目之间的联系，需要对应用进行注册</span>
</code></pre></div><h2 id="23新建视图">2.3新建视图</h2>
<h3 id="231简单视图">2.3.1简单视图</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#1.进入polls/views.py</span>
<span style="color:#f92672">from</span> django.http <span style="color:#f92672">import</span> HttpResponse

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">index</span>(request):
    <span style="color:#66d9ef">return</span> HttpResponse(<span style="color:#e6db74">&#34;Hello, world. You&#39;re at the polls index.&#34;</span>)

<span style="color:#75715e">#2.创建URLconf， 在polls目录中新建urls.py</span>
<span style="color:#f92672">from</span> django.urls <span style="color:#f92672">import</span> path

<span style="color:#f92672">from</span> . <span style="color:#f92672">import</span> views

urlpatterns <span style="color:#f92672">=</span> [
    path(<span style="color:#e6db74">&#39;&#39;</span>, views<span style="color:#f92672">.</span>index, name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;index&#39;</span>),
]

<span style="color:#75715e">#3.指定polls.urls模块，在项目文件中的urls中插入</span>
<span style="color:#f92672">from</span> django.contrib <span style="color:#f92672">import</span> admin
<span style="color:#f92672">from</span> django.urls <span style="color:#f92672">import</span> include, path

urlpatterns <span style="color:#f92672">=</span> [
    path(<span style="color:#e6db74">&#39;polls/&#39;</span>, include(<span style="color:#e6db74">&#39;polls.urls&#39;</span>)),		
    <span style="color:#75715e">#path():必须含（route，view) 可选参数 kwargs，name</span>
    <span style="color:#75715e">#kwargs，字典传递</span>
    <span style="color:#75715e">#name：全局引用</span>
    path(<span style="color:#e6db74">&#39;admin/&#39;</span>, admin<span style="color:#f92672">.</span>site<span style="color:#f92672">.</span>urls),
]

</code></pre></div><h3 id="232视图完善">2.3.2视图完善</h3>
<p>视图内内容必须包含：</p>
<ul>
<li>返回一个包含页面内容的httpResponse对象</li>
<li>或者抛出异常</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#展示数据库以发布日期排序的5个问题</span>
<span style="color:#f92672">from</span> django.http <span style="color:#f92672">import</span> HttpResponse
<span style="color:#f92672">from</span> .models <span style="color:#f92672">import</span> Question

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">index</span>(request):
    latest_question_list <span style="color:#f92672">=</span> Question<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>order_by(<span style="color:#e6db74">&#39;-pub_date&#39;</span>)[:<span style="color:#ae81ff">5</span>]
    output <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;, &#39;</span><span style="color:#f92672">.</span>join([q<span style="color:#f92672">.</span>question_text <span style="color:#66d9ef">for</span> q <span style="color:#f92672">in</span> latest_question_list])
    <span style="color:#66d9ef">return</span> HttpResponse(output)

<span style="color:#75715e"># view使用模板</span>
<span style="color:#f92672">from</span> django.http <span style="color:#f92672">import</span> HttpResponse
<span style="color:#f92672">from</span> django.template <span style="color:#f92672">import</span> loader
<span style="color:#f92672">from</span> .models <span style="color:#f92672">import</span> Question

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">index</span>(request):
    latest_question_list <span style="color:#f92672">=</span> Question<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>order_by(<span style="color:#e6db74">&#39;-pub_date&#39;</span>)[:<span style="color:#ae81ff">5</span>]
    template <span style="color:#f92672">=</span> loader<span style="color:#f92672">.</span>get_template(<span style="color:#e6db74">&#39;polls/index.html&#39;</span>)
    context <span style="color:#f92672">=</span> {
        <span style="color:#e6db74">&#39;latest_question_list&#39;</span>: latest_question_list,
    }
    <span style="color:#66d9ef">return</span> HttpResponse(template<span style="color:#f92672">.</span>render(context, request))
</code></pre></div><h3 id="233添加template目录">2.3.3添加template目录</h3>
<p>在app目录中创建一个template目录，Django将会在该目录下查找文件模板文件</p>
<p>你项目的 TEMPLATES 配置项描述了 Django 如何载入和渲染模板。默认的设置文件设置了 DjangoTemplates 后端，并将 APP_DIRS 设置成了 True。这一选项将会让 DjangoTemplates 在每个 INSTALLED_APPS 文件夹中寻找 &ldquo;templates&rdquo; 子目录。这就是为什么尽管我们没有像在第二部分中那样修改 DIRS 设置，Django 也能正确找到 polls 的模板位置的原因。</p>
<p>在刚创建的目录中在添加一个app目录在其中添加app中需要的.html文件，该文件可以直接使用appname/xxx.html直接引用</p>
<h3 id="234快捷函数">2.3.4快捷函数</h3>
<ul>
<li>render()</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#使用render()替换loader和httpResponse</span>
<span style="color:#f92672">from</span> django.shortcuts <span style="color:#f92672">import</span> render
<span style="color:#f92672">from</span> .models <span style="color:#f92672">import</span> Question

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">index</span>(request):
    latest_question_list <span style="color:#f92672">=</span> Question<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>order_by(<span style="color:#e6db74">&#39;-pub_date&#39;</span>)[:<span style="color:#ae81ff">5</span>]
    context <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;latest_question_list&#39;</span>: latest_question_list}
    <span style="color:#66d9ef">return</span> render(request, <span style="color:#e6db74">&#39;polls/index.html&#39;</span>, context)
</code></pre></div><ul>
<li>get_object_0r_404()抛出异常</li>
</ul>
<p>尝试用 get() 函数获取一个对象，如果不存在就抛出 Http404 错误也是一个普遍的流程。Django 也提供了一个快捷函数，下面是修改后的详情 detail() 视图代码：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">
<span style="color:#f92672">from</span> django.shortcuts <span style="color:#f92672">import</span> get_object_or_404, render
<span style="color:#f92672">from</span> .models <span style="color:#f92672">import</span> Question
<span style="color:#75715e"># ...</span>
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">detail</span>(request, question_id):
    question <span style="color:#f92672">=</span> get_object_or_404(Question, pk<span style="color:#f92672">=</span>question_id)
    <span style="color:#66d9ef">return</span> render(request, <span style="color:#e6db74">&#39;polls/detail.html&#39;</span>, {<span style="color:#e6db74">&#39;question&#39;</span>: question})
</code></pre></div><h2 id="24数据库配置">2.4数据库配置</h2>
<h3 id="241settingpy配置">2.4.1setting.py配置</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#e6db74">&#34;&#34;&#34;
</span><span style="color:#e6db74">setting.py配置数据库
</span><span style="color:#e6db74">DATABASES：
</span><span style="color:#e6db74">ENGINE -- 可选值有
</span><span style="color:#e6db74">&#39;django.db.backends.sqlite3&#39;，&#39;django.db.backends.postgresql&#39;，&#39;django.db.backends.mysql&#39;，或 &#39;django.db.backends.oracle&#39;
</span><span style="color:#e6db74">NAME：数据库名称
</span><span style="color:#e6db74">注：sqlite外数据库，需要确保数据库存在
</span><span style="color:#e6db74">&#34;&#34;&#34;</span>
<span style="color:#75715e">#设置时区 TIME_ZONE</span>

<span style="color:#75715e">#生成initial文件，据库迁移文件</span>
python3 manage<span style="color:#f92672">.</span>py makemigrations xxx(app名)

<span style="color:#75715e">#该命令查看setting.py中的数据库设置和app中的附带数据库迁移创建任何必须的数据库表</span>
python3 manage<span style="color:#f92672">.</span>py migrate <span style="color:#75715e">#该命令只会对已声明应用进行数据库迁移</span>



</code></pre></div><h3 id="242创建模型">2.4.2创建模型</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#对象类描述</span>
<span style="color:#f92672">from</span> django.db <span style="color:#f92672">import</span> models

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Question</span>(models<span style="color:#f92672">.</span>Model):
    question_text <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>)
    pub_date <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>DateTimeField(<span style="color:#e6db74">&#39;date published&#39;</span>)

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Choice</span>(models<span style="color:#f92672">.</span>Model):
    question <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>ForeignKey(Question, on_delete<span style="color:#f92672">=</span>models<span style="color:#f92672">.</span>CASCADE)
    choice_text <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>)
    votes <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>IntegerField(default<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>)
</code></pre></div><h3 id="243其他配置">2.4.3其他配置</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> INSTALLED_APPS 设置项。
        django.contrib.admin -- 管理员站点， 你很快就会使用它。
        django.contrib.auth -- 认证授权系统。
        django.contrib.contenttypes -- 内容类型框架。
        django.contrib.sessions -- 会话框架。
        django.contrib.messages -- 消息框架。
        django.contrib.staticfiles -- 管理静态文件的框架。
</code></pre></div><h3 id="244激活模型">2.4.4激活模型</h3>
<p>在项目文件的setting.py中的INSTALLED_APPS 子项中加入点式路径如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">INSTALLED_APPS <span style="color:#f92672">=</span> [
    <span style="color:#e6db74">&#39;polls.apps.PollsConfig&#39;</span>,	<span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">应用的</span>config路径
    <span style="color:#e6db74">&#39;django.contrib.admin&#39;</span>,
    <span style="color:#e6db74">&#39;django.contrib.auth&#39;</span>,
    <span style="color:#e6db74">&#39;django.contrib.contenttypes&#39;</span>,
    <span style="color:#e6db74">&#39;django.contrib.sessions&#39;</span>,
    <span style="color:#e6db74">&#39;django.contrib.messages&#39;</span>,
    <span style="color:#e6db74">&#39;django.contrib.staticfiles&#39;</span>,
]

<span style="color:#75715e">#检测对模型文件的修改，并把修改的部分存储为一次迁移</span>
 python manage<span style="color:#f92672">.</span>py makemigrations polls
<span style="color:#75715e">#接受一个迁移命令并返回对应的sql</span>
 python manage<span style="color:#f92672">.</span>py sqlmigrate polls <span style="color:#ae81ff">0001</span>
<span style="color:#75715e">#对项目进行检查。不会多数据库进行任何操作</span>
python manage<span style="color:#f92672">.</span>py check 
</code></pre></div><h1 id="3django管理页面">3、Django管理页面</h1>
<p>Django全自动的根据模型创建后台界面</p>
<h2 id="31创建管理员用户">3.1创建管理员用户</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">python3 manage.py createsuperuser
</code></pre></div><h2 id="32将app加入管理页面">3.2将app加入管理页面</h2>
<p>在app中的admin.py 中进行如下修改：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#加入Question对象的管理</span>
<span style="color:#f92672">from</span> django.contrib <span style="color:#f92672">import</span> admin
<span style="color:#f92672">from</span> .models <span style="color:#f92672">import</span> Question

admin<span style="color:#f92672">.</span>site<span style="color:#f92672">.</span>register(Question)
</code></pre></div><h1 id="4优化">4、优化</h1>
<h2 id="41添加命名空间">4.1添加命名空间</h2>
<p>在app中的urls.py中加入app_name=&quot;xxx&quot;设置命名空间</p>
<p>即可在视图中去除硬编码的URL，如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-html" data-lang="html">//使用硬编码
&lt;<span style="color:#f92672">li</span>&gt;&lt;<span style="color:#f92672">a</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;/polls/{{ question.id }}/&#34;</span>&gt;{{ question.question_text }}&lt;/<span style="color:#f92672">a</span>&gt;&lt;/<span style="color:#f92672">li</span>&gt;
//通过命名空间
&lt;<span style="color:#f92672">li</span>&gt;&lt;<span style="color:#f92672">a</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;{% url &#39;detail&#39; question.id %}&#34;</span>&gt;{{ question.question_text }}&lt;/<span style="color:#f92672">a</span>&gt;&lt;/<span style="color:#f92672">li</span>&gt;
</code></pre></div><h2 id="42通用视图">4.2通用视图</h2>
<ul>
<li>转换URLconfig</li>
<li>删除一些旧的、不要的视图</li>
<li>基于Django的通用视图引入新视图</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#改良URLconfig</span>
<span style="color:#f92672">from</span> django.urls <span style="color:#f92672">import</span> path
<span style="color:#f92672">from</span> . <span style="color:#f92672">import</span> views

app_name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;polls&#39;</span>
urlpatterns <span style="color:#f92672">=</span> [
    path(<span style="color:#e6db74">&#39;&#39;</span>, views<span style="color:#f92672">.</span>IndexView<span style="color:#f92672">.</span>as_view(), name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;index&#39;</span>),
    path(<span style="color:#e6db74">&#39;&lt;int:pk&gt;/&#39;</span>, views<span style="color:#f92672">.</span>DetailView<span style="color:#f92672">.</span>as_view(), name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;detail&#39;</span>),
    path(<span style="color:#e6db74">&#39;&lt;int:pk&gt;/results/&#39;</span>, views<span style="color:#f92672">.</span>ResultsView<span style="color:#f92672">.</span>as_view(), name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;results&#39;</span>),
    path(<span style="color:#e6db74">&#39;&lt;int:question_id&gt;/vote/&#39;</span>, views<span style="color:#f92672">.</span>vote, name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;vote&#39;</span>),
]
<span style="color:#75715e">#改良视图</span>
<span style="color:#f92672">from</span> django.http <span style="color:#f92672">import</span> HttpResponseRedirect
<span style="color:#f92672">from</span> django.shortcuts <span style="color:#f92672">import</span> get_object_or_404, render
<span style="color:#f92672">from</span> django.urls <span style="color:#f92672">import</span> reverse
<span style="color:#f92672">from</span> django.views <span style="color:#f92672">import</span> generic
<span style="color:#f92672">from</span> .models <span style="color:#f92672">import</span> Choice, Question

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">IndexView</span>(generic<span style="color:#f92672">.</span>ListView):
    template_name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;polls/index.html&#39;</span>
    context_object_name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;latest_question_list&#39;</span>

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get_queryset</span>(self):
        <span style="color:#e6db74">&#34;&#34;&#34;Return the last five published questions.&#34;&#34;&#34;</span>
        <span style="color:#66d9ef">return</span> Question<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>order_by(<span style="color:#e6db74">&#39;-pub_date&#39;</span>)[:<span style="color:#ae81ff">5</span>]

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">DetailView</span>(generic<span style="color:#f92672">.</span>DetailView):
    model <span style="color:#f92672">=</span> Question
    template_name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;polls/detail.html&#39;</span>

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ResultsView</span>(generic<span style="color:#f92672">.</span>DetailView):
    model <span style="color:#f92672">=</span> Question
    template_name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;polls/results.html&#39;</span>

</code></pre></div><h1 id="5自动化测试">5.自动化测试</h1>
<p>略</p>
<h1 id="6自定义风格后台表单">6、自定义风格，后台表单</h1>
<ul>
<li>在app中创建static目录，再在其中创建app目录，在目录里可添加css，imges等</li>
<li>后台表单中使用fieldsets， TabularInline<code>（替代 </code>StackedInline ）等</li>
<li>等等</li>
</ul>
<h1 id="回顾">回顾</h1>
  </div>
  <footer>
    <ul class="stats">
  
    
    
      <li class="categories">
        <ul>
          
            
            <li><a class="article-category-link" href="https://hhs44.github.io/categories/python">python</a></li>
          
        </ul>
      </li>
    
  
  
    
    
      <li class="tags">
        <ul>
          
            
            <li><a class="article-category-link" href="https://hhs44.github.io/tags/django">Django</a></li>
          
        </ul>
      </li>
    
  
</ul>

  </footer>
</article>

    <article class="post">
      
<div>
  <h2>说些什么</h2>
    <form id="post-js-form" class="post-new-comment" method="POST">
      
      <h5 class='post-reply-notice hidden'>
        <span class='post-reply-arrow'></span><span class='post-reply-name'></span>
      </h5>

      
      <input type="hidden" name="options[entryId]" value="859c51e13d63d994da1d9e81a732a83e">
      <input type='hidden' name='fields[replyThread]' value=''>
      <input type='hidden' name='fields[replyID]' value=''>
      <input type='hidden' name='fields[replyName]' value=''>

      
      <input required name='fields[name]' type='text' placeholder='您的名字（必填）'>
      <input name='fields[website]' type='text' placeholder='您的网站'>
      <input required name='fields[email]' type='email' placeholder='您的电邮地址（必填，用于 Gravatar 头象）'>
      <textarea required name='fields[body]' placeholder='您的留言。可使用 Markdown。（搜索 &#39;Markdown 語法速查&#39;）' rows='10'></textarea>

      
      

      
      <p class='post-submit-notice'>
        <strong class='post-submit-notice-text submit-success hidden'>感谢您的评论！被批准后它会立即在此站点展示。</strong>
        <strong class='post-submit-notice-text submit-failed hidden'>很抱歉，您的提交存在错误。请确保所有必填字段都已填写正确，然后再试一次。</strong>
      </p>

      
      <input type='submit' value='送出' class='button'>
      <input type='submit' value='已送出' class='hidden button' disabled>
      <input type='reset' value='重设' class='button'>
    </form>
</div>


<div>
  <h2>评论</h2>

  
    
      <p>还沒有留言。</p>
    
  
</div>

    </article>


<div class="pagination">
  
    <a href="/blog/ubuntu-%E5%9B%BE%E5%BD%A2%E7%95%8C%E9%9D%A2%E7%B3%BB%E7%BB%9F%E4%BF%AE%E5%A4%8D/" class="button"><div class="previous"><div>ubuntu_图形界面系统修复</div></div></a>
  
  
    <a href="/blog/%E8%80%81%E7%9F%A5%E8%AF%86%E8%BF%81%E7%A7%BB%E4%B8%80/" class="button"><div class="next"><div>老知识迁移（一）</div></div></a>
  
</div>


      </main>
      <div class="post-toc">
     
    <aside>
        <header>
        <h4>老知识迁移（二）的字数: 495</h4> 
        </header><nav id="TableOfContents">
  <ul>
    <li><a href="#1django综述">1、Django综述</a>
      <ul>
        <li><a href="#11框架结构">1.1框架结构</a></li>
      </ul>
    </li>
    <li><a href="#2第一个django应用">2、第一个Django应用</a>
      <ul>
        <li><a href="#21文件说明">2.1文件说明</a></li>
        <li><a href="#heading"></a></li>
        <li><a href="#22新建app">2.2新建app</a></li>
        <li><a href="#23新建视图">2.3新建视图</a>
          <ul>
            <li><a href="#231简单视图">2.3.1简单视图</a></li>
            <li><a href="#232视图完善">2.3.2视图完善</a></li>
            <li><a href="#233添加template目录">2.3.3添加template目录</a></li>
            <li><a href="#234快捷函数">2.3.4快捷函数</a></li>
          </ul>
        </li>
        <li><a href="#24数据库配置">2.4数据库配置</a>
          <ul>
            <li><a href="#241settingpy配置">2.4.1setting.py配置</a></li>
            <li><a href="#242创建模型">2.4.2创建模型</a></li>
            <li><a href="#243其他配置">2.4.3其他配置</a></li>
            <li><a href="#244激活模型">2.4.4激活模型</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#3django管理页面">3、Django管理页面</a>
      <ul>
        <li><a href="#31创建管理员用户">3.1创建管理员用户</a></li>
        <li><a href="#32将app加入管理页面">3.2将app加入管理页面</a></li>
      </ul>
    </li>
    <li><a href="#4优化">4、优化</a>
      <ul>
        <li><a href="#41添加命名空间">4.1添加命名空间</a></li>
        <li><a href="#42通用视图">4.2通用视图</a></li>
      </ul>
    </li>
    <li><a href="#5自动化测试">5.自动化测试</a></li>
    <li><a href="#6自定义风格后台表单">6、自定义风格，后台表单</a></li>
    <li><a href="#回顾">回顾</a></li>
  </ul>
</nav></aside>
    <a href="#" id="toc-toggle"></a>
     
</div>
      <section id="site-sidebar">
  
    <section id="recent-posts">
      <header>
        <h1>最新文章</h1>
      </header>
      
      <article class="mini-post">
        <section>
          
  <a href="/blog/css%E5%AD%A6%E4%B9%A0%E4%B8%80/" class="image featured">
    
      <img src="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/20200331114355.png" alt="">
    
  </a>


        </section>
        <header>
          <h1><a href="/blog/css%E5%AD%A6%E4%B9%A0%E4%B8%80/">CSS学习一</a></h1>
          <time class="published" datetime="">March 31, 2020</time>
        </header>
      </article>
      
      <article class="mini-post">
        <section>
          
  <a href="/blog/html%E5%AD%A6%E4%B9%A0%E4%B8%80/" class="image featured">
    
      <img src="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/20200331114355.png" alt="">
    
  </a>


        </section>
        <header>
          <h1><a href="/blog/html%E5%AD%A6%E4%B9%A0%E4%B8%80/">HTML学习一</a></h1>
          <time class="published" datetime="">March 31, 2020</time>
        </header>
      </article>
      
      <article class="mini-post">
        <section>
          
  <a href="/blog/web%E5%9F%BA%E7%A1%80/" class="image featured">
    
      <img src="https://raw.githubusercontent.com/hhs44/cloudimg/master/img/20200331114355.png" alt="">
    
  </a>


        </section>
        <header>
          <h1><a href="/blog/web%E5%9F%BA%E7%A1%80/">web基础</a></h1>
          <time class="published" datetime="">March 31, 2020</time>
        </header>
      </article>
      
      
        <a href="/blog/" class="button">查看更多</a>
      
    </section>
  

  
    
      <section id="categories">
        <header>
          <h1><a href="/categories">分类</a></h1>
        </header>
        <ul>
          
            
          
          
          <li>
            
              <a href="/categories/python/">python<span class="count">6</span></a>
            
          
          <li>
            
              <a href="/categories/linux/">linux<span class="count">4</span></a>
            
          
          <li>
            
              <a href="/categories/web/">web<span class="count">4</span></a>
            
          
          <li>
            
              <a href="/categories/hexo/">hexo<span class="count">3</span></a>
            
          
          <li>
            
              <a href="/categories/jetbrain/">jetbrain<span class="count">2</span></a>
            
          
          <li>
            
              <a href="/categories/test/">test<span class="count">2</span></a>
            
          
          <li>
            
              <a href="/categories/%E5%89%8D%E7%AB%AF/">前端<span class="count">2</span></a>
            
          
          <li>
            
              <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库<span class="count">2</span></a>
            
          
          <li>
            
              <a href="/categories/conda/">conda<span class="count">1</span></a>
            
          
          <li>
            
              <a href="/categories/development/">development<span class="count">1</span></a>
            
          
          <li>
            
              <a href="/categories/golang/">golang<span class="count">1</span></a>
            
          
          <li>
            
              <a href="/categories/java/">java<span class="count">1</span></a>
            
          
          <li>
            
              <a href="/categories/syntax/">syntax<span class="count">1</span></a>
            
          
          <li>
            
              <a href="/categories/%E9%80%9A%E4%BF%A1/">通信<span class="count">1</span></a>
            
          
          </li>
        </ul>
      </section>
    
  

  
    <section id="mini-bio">
      <header>
        <h1>关于</h1>
      </header>
      <p>test</p>
      <footer>
        <a href="/about" class="button">了解更多</a>
      </footer>
    </section>
  
</section>

      <footer id="site-footer">
  
      <ul class="socnet-icons">
        

        <li><a href="//github.com/hhs44/hhs44.github.io" target="_blank" rel="noopener" title="GitHub" class="fab fa-github"></a></li>






<li><a href="//flickr.com/photos/example" target="_blank" rel="noopener" title="Flickr" class="fab fa-flickr"></a></li>




<li><a href="//linkedin.com/in/example" target="_blank" rel="noopener" title="LinkedIn" class="fab fa-linkedin"></a></li>
<li><a href="//linkedin.com/company/examplebusiness" target="_blank" rel="noopener" title="LinkedIn Company" class="fab fa-linkedin"></a></li>



<li><a href="//facebook.com/example" target="_blank" rel="noopener" title="Facebook" class="fab fa-facebook"></a></li>
<li><a href="//reddit.com/user/example" target="_blank" rel="noopener" title="Reddit" class="fab fa-reddit"></a></li>





<li><a href="weixin://contacts/profile/22" target="_blank" rel="noopener" title="WeChat" class="fab fa-weixin"></a></li>
<li><a href="//wpa.qq.com/msgrd?v=3&amp;uin=1159986871&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener" title="QQ" class="fab fa-qq"></a></li>
<li><a href="//instagram.com/example" target="_blank" rel="noopener" title="Instagram" class="fab fa-instagram"></a></li>

<li><a href="//twitter.com/example" target="_blank" rel="noopener" title="Twitter" class="fab fa-twitter"></a></li>


<li><a href="//pinterest.com/example" target="_blank" rel="noopener" title="Pinterest" class="fab fa-pinterest-p"></a></li>
<li><a href="//telegram.me/example" target="_blank" rel="noopener" title="telegram" class="fab fa-telegram"></a></li>



<li><a href="//researchgate.net/profile/example" target="_blank" rel="noopener" title="Research Gate"><i class="ai ai-researchgate"></i></a></li>
<li><a href="//keybase.io/example" target="_blank" rel="noopener" title="keybase" class="fab fa-keybase"></a></li>
<li><a href="//example.com/@example" target="_blank" rel="noopener" title="mastodon" class="fab fa-mastodon"></a></li>
<li><a href="mailto:1159986871@qq.com" target="_blank" title="Email" class="far fa-envelope"></a></li>

      </ul>
  
  <p class="copyright">
    
      &copy; 2020
      
        Huanghs blog
      
    . <br>
    主题: <a href='https://themes.gohugo.io/hugo-future-imperfect-slim/' target='_blank' rel='noopener'>Hugo Future Imperfect Slim</a><br>移植自 <a href='https://html5up.net/future-imperfect' target='_blank' rel='noopener'>HTML5 UP</a> | 由 <a href='https://gohugo.io/' target='_blank' rel='noopener' title='0.66.0'>Hugo</a> 驱动
  </p>
</footer>
<a id="back-to-top" href="#" class="fas fa-arrow-up fa-2x"></a>

      <script src="/js/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script><script src="/js/bundle.min.b17fef5a58c033a8a59f57c83aa975a251aecff921cbd36c12e975d530fa023b.js" integrity="sha256-sX/vWljAM6iln1fIOql1olGuz/khy9NsEul11TD6Ajs="></script>
    <script src="/js/add-on.js"></script>
    </div>
  </body>
</html>
